package org.one.afternoon.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.one.afternoon.common.Result;
import org.one.afternoon.domain.Example;
import org.one.afternoon.service.ExampleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/example")
public class ExampleController {
    @Autowired
    private ExampleService exampleService;

    @CrossOrigin // 跨域访问
    @GetMapping("/all")
    public Result<List<Example>> findAll() {
        List<Example> exampleList = exampleService.list(null);
        return Result.success(exampleList);
    }


    @CrossOrigin
    @PostMapping("/queryOne")
    @ResponseBody
    public Result<List<Example>> queryOne(@RequestBody Example example) {

        LambdaQueryWrapper<Example> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotBlank(example.getAttribute1()), Example::getAttribute1, example.getAttribute1())
                .like(StringUtils.isNotBlank(example.getAttribute2()), Example::getAttribute2, example.getAttribute2())
                .like(StringUtils.isNotBlank(example.getAttribute3()), Example::getAttribute3, example.getAttribute3())
                .like(StringUtils.isNotBlank(example.getAttribute4()), Example::getAttribute4, example.getAttribute4());
        List<Example> list = exampleService.list(lambdaQueryWrapper);
        return Result.success(list);
    }

    @CrossOrigin
    @PostMapping("/insertOne")
    @ResponseBody
    public Result<String> addOne(@RequestBody Example example) {
        boolean save = exampleService.save(example);
        Result<String> result;
        if (!save) {
            result = Result.error("添加用户失败");
        } else {
            result = Result.success("删除成功");
        }
        return result;
    }

    @CrossOrigin
    @PostMapping("/deleteOne")
    @ResponseBody
    public Result<String> deleteOne(@RequestParam Integer index) {
        exampleService.removeById(index);
        Result<String> result = Result.success("删除成功");
        return result;
    }

    @CrossOrigin
    @PostMapping("/updateOne")
    @ResponseBody
    public Result<String> updateOne(@RequestBody Example example) {
        LambdaUpdateWrapper<Example> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(StringUtils.isNotBlank(example.getAttribute1()), Example::getAttribute1, example.getAttribute1())
                .set(StringUtils.isNotBlank(example.getAttribute2()), Example::getAttribute2, example.getAttribute2())
                .set(StringUtils.isNotBlank(example.getAttribute3()), Example::getAttribute3, example.getAttribute3())
                .set(StringUtils.isNotBlank(example.getAttribute4()), Example::getAttribute4, example.getAttribute4());

        exampleService.update(null, lambdaUpdateWrapper);
        Result<String> result = Result.success("修改成功");
        return result;
    }
}
